const {ipcRenderer, webFrame} = require('electron')

webFrame.setZoomLevel(1)
webFrame.setVisualZoomLevelLimits(1, 1)
webFrame.setZoomFactor(1)

let winId;
ipcRenderer.on('init', (event, fromWinId, payload) => {
    // console.log('init', payload)
    if (payload && payload.image) {
        let img = document.querySelector("#preview")
        img.src = payload.image;
    }
    winId = fromWinId;
});

ipcRenderer.on('will-resize', (event, fromWinId, newBounds) => {
    let img = window.document.querySelector("#preview");
    img.style.width = '100%';
    img.style.height = '100%';
    let scale = img.width / (img.height * 1.0)
    let height = newBounds.width / scale;
    // console.log('will-resize', newBounds, scale, height)
    window.resizeTo(newBounds.width, height);
})

ipcRenderer.on('focus', (event, fromWinId) => {
    document.body.classList.add('body-focus')
})

ipcRenderer.on('blur', (event, fromWinId) => {
    document.body.classList.remove('body-focus')
})

ipcRenderer.on('pinChange', (event, fromWinId, isPin) => {
    document.dispatchEvent(new CustomEvent('pinChange', {detail: isPin}));
})
ipcRenderer.on('reduction', (event, fromWinId) => {
    document.body.classList.add("body-hover")
})

window.resize = (changed, proportion) => {
    ipcRenderer.send('SendTo', winId, 'resize', changed, proportion);
}
window.moveBounds = (x, y, width, height) => {
    ipcRenderer.send('SendTo', winId, 'moveBounds', x, y, width, height);
}

window.copyImage = () => {
    ipcRenderer.send('SendTo', winId, 'copyImage');
}

window.saveImage = () => {
    ipcRenderer.send('SendTo', winId, 'saveImage');
}

window.modifyOpacity = (opacity) => {
    ipcRenderer.send('SendTo', winId, 'modifyOpacity', opacity);
}

window.triggerPin = () => {
    ipcRenderer.send('SendTo', winId, 'triggerPin');
}
